import QtQuick 1.1
import com.meego 1.0
import "CommomUtil.js" as Util
import "Modulelib.js" as ModuleUtil
import "const.js" as Const

Page {
    id: dealer_cars_page

    tools: ModuleToolBar{}

    orientationLock : PageOrientation.LockPortrait


    TitleBar{
           id: dealer_cars_title
           anchors.top: dealer_cars_page.top; anchors.left: dealer_cars_page.left
           width: dealer_cars_page.width; height: 71
           titleName: ModuleUtil.current_dealer_name
    }

    Component{
        id: carsShowDelegate
        Item {
            width: 450; height: 155
            Rectangle{
                anchors.fill: parent
                gradient: Gradient {
                    GradientStop { position: 0.0; color: "#e5edf9" }
                    GradientStop { position: 0.5; color: "#c4c8cf" }
                    GradientStop { position: 1; color: "#e5edf9" }
                }
                border{color: "#abb2b3"; width: 1}
                Column{
                    spacing: 9
                    Item {
                        width: 450; height: 1
                    }
                    Row{
                        spacing: 8
                        Item{
                            width: 1; height: 125
                        }
                        Rectangle{
                            color: "#e5edf9"
                            width: 120; height: 95
                            Image {
                                anchors.centerIn: parent
                                width: 100; height: 75
                                fillMode: Image.PreserveAspectFit
                                source: specImage
                            }
                        }
                        Column{
                            spacing: 5
                            Text {
                                text: specName
                                font{bold: true; pointSize: 18}
                                color: "#000000"
                                horizontalAlignment: Text.AlignLeft
                            }
                            Text {
                                text: "报价时间:" + specTime
                                font{bold: true; pointSize: 16}
                                color: "#abb2b3"
                                horizontalAlignment: Text.AlignLeft
                            }
                            Text {
                                text: "厂商指导价:" + specPrice / 10000 + "万"
                                font{bold: true; pointSize: 16}
                                color: "#abb2b3"
                                horizontalAlignment: Text.AlignLeft
                            }
                            Row{
                                spacing: 2
                                Text {
                                    text: "经销商报价:"
                                    font{bold: true; pointSize: 16}
                                    color: "#93a0b5"
                                    horizontalAlignment: Text.AlignLeft
                                }
                                Text {
                                    text: specDPrice / 10000 + "万"
                                    font{bold: true; pointSize: 17}
                                    color: "red"
                                    horizontalAlignment: Text.AlignLeft
                                }
                            }
                        }
                    }
                    Item {
                        width: 450; height: 10
                    }
                }
            }
        }
    }

    Component{
        id: carsHeaderDelegate
        Item {
            width: 450; height: 50
            Rectangle{
                color: Const.Color.TitleColor
                border{color: "white"; width: 1}
                Row{
                    Item{
                        width: 20; height: 50
                    }
                    Text{
                        text: section
                        width: 430; height: 50
                        font{bold: true; pointSize: 18}
                        color: Const.Color.white
                        horizontalAlignment: Text.AlignLeft
                        verticalAlignment: Text.AlignVCenter
                    }
                }
            }

        }
    }

    Rectangle{
        id: cars_list_rect
        width: parent.width; height: parent.height - 71
        anchors{left: parent.left; top: dealer_cars_title.bottom;}
        gradient: Gradient {
            GradientStop { position: 0.0; color: "white" }
            GradientStop { position: 1; color: "#c0c5c6" }
        }
        Rectangle{
            anchors{fill: parent; leftMargin: 15; rightMargin: 15}
            FlickListView{
                id: cars_list
                anchors.fill: parent
                model: dealer_cars_data
                delegate: carsShowDelegate

                section.property: "seriesName"
                section.criteria: ViewSection.FullString
                section.delegate: carsHeaderDelegate
            }
        }
    }


    ListModel{
        id: dealer_cars_data
    }


    BusyLoading{
        id: loading
        anchors.centerIn: parent
        visible:false
    }

    Component.onCompleted: {
        loading.loading();
        dealer_cars_page.getData();
    }

    function getData(){
        Util.getXmlStringByUrl(ModuleUtil.getDealerCarsUrl(),callback);
    }

    function callback(responseXml){
        var a = responseXml.documentElement;
        for(var i = 0; i < a.childNodes[0].childNodes.length; ++i){
        var seriesId = a.childNodes[0].childNodes[i].childNodes[0].childNodes[0].nodeValue;
        var seriesName = a.childNodes[0].childNodes[i].childNodes[1].childNodes[0].nodeValue;
        for(var j = 2; j < a.childNodes[0].childNodes[i].childNodes[2].childNodes.length; ++j){
                 var specId = a.childNodes[0].childNodes[i].childNodes[2].childNodes[j].childNodes[0].childNodes[0].nodeValue;
                 var specName = a.childNodes[0].childNodes[i].childNodes[2].childNodes[j].childNodes[1].childNodes[0].nodeValue;
                 var specPrice = a.childNodes[0].childNodes[i].childNodes[2].childNodes[j].childNodes[2].childNodes[0].nodeValue;
                 var specDPrice = a.childNodes[0].childNodes[i].childNodes[2].childNodes[j].childNodes[3].childNodes[0].nodeValue;
                 var specImage = a.childNodes[0].childNodes[i].childNodes[2].childNodes[j].childNodes[4].childNodes[0].nodeValue;
                 var specTime = a.childNodes[0].childNodes[i].childNodes[2].childNodes[j].childNodes[5].childNodes[0].nodeValue;
            dealer_cars_data.append({"seriesId" : seriesId , "seriesName" :seriesName , "specId" :specId , "specName" :specName , "specPrice" :specPrice ,
                                    "specDPrice" :specDPrice , "specImage" :specImage , "specTime":specTime});
        }

        }
        loading.finish();
    }
}
